{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#通过与大数据框的相对位置，获得图上的红框的绝对坐标，已剔除小图处的红框\n",
    "def got_rect_locations():\n",
    "    mxd = mp.MapDocument('CURRENT')\n",
    "    lsd = mp.ListDataFrames(mxd)\n",
    "    relative_locations = []\n",
    "    rect_locations = []\n",
    "    #获取大图范围\n",
    "    max_df = max(arcpy.mapping.ListLayoutElements(mxd, \"DATAFRAME_ELEMENT\"),key = lambda x:x.elementWidth)\n",
    "    left,bottom,right,top = max_df.elementPositionX,max_df.elementPositionY,max_df.elementPositionX + max_df.elementWidth,max_df.elementPositionY + max_df.elementHeight\n",
    "    \n",
    "    #获取小图范围\n",
    "    frame_num = len(arcpy.mapping.ListLayoutElements(mxd, \"DATAFRAME_ELEMENT\"))\n",
    "    if frame_num > 1:\n",
    "        minor_df = min(arcpy.mapping.ListLayoutElements(mxd, \"DATAFRAME_ELEMENT\"),key = lambda x:x.elementWidth)\n",
    "        minor_left,minor_bottom,minor_right,minor_top = minor_df.elementPositionX,minor_df.elementPositionY,minor_df.elementPositionX + minor_df.elementWidth,minor_df.elementPositionY + minor_df.elementHeight\n",
    "    else:#没有小图\n",
    "        minor_left,minor_bottom,minor_right,minor_top = 0,0,0,0\n",
    "        \n",
    "    for elm in arcpy.mapping.ListLayoutElements(mxd, \"GRAPHIC_ELEMENT\"): \n",
    "        #剔除小图红框\n",
    "        if  minor_right-elm.elementWidth > elm.elementPositionX >  minor_left and  minor_top -  elm.elementHeight > elm.elementPositionY >  minor_bottom:\n",
    "            continue\n",
    "            \n",
    "        #考虑红框的宽度高度\n",
    "        if right-elm.elementWidth > elm.elementPositionX > left and top -  elm.elementHeight > elm.elementPositionY > bottom:\n",
    "            relative_locations.append((elm.elementPositionX,elm.elementPositionY,elm.elementPositionX+elm.elementWidth,elm.elementPositionY+elm.elementHeight))\n",
    "    \n",
    "    #计算绝对坐标\n",
    "    minX,minY,maxX,maxY =  max_df.extent.XMin,max_df.extent.YMin,max_df.extent.XMax,max_df.extent.YMax\n",
    "    for i in relative_locations:\n",
    "        absolute_minX = ((i[0] - left)/(right-left))*(maxX-minX)+minX\n",
    "        absolute_maxX = ((i[2] - left)/(right-left))*(maxX-minX)+minX\n",
    "        absolute_minY = ((i[1] - bottom)/(top-bottom))*(maxY-minY)+minY   \n",
    "        absolute_maxY = ((i[3] - bottom)/(top-bottom))*(maxY-minY)+minY\n",
    "        rect_locations.append((absolute_minX,absolute_minY,absolute_maxX,absolute_maxY))\n",
    "    return rect_locations    \n",
    "\n",
    "#根据每个框主要包含的图斑，得到框<--->地域的对应关系\n",
    "def release_rect(rect_locations,polygonName):\n",
    "    k = [[0 for i in range(len(rect_locations))] for i in range(len(rect_locations))]\n",
    "    \n",
    "    with arcpy.da.SearchCursor(polygonName, [\"SHAPE@\",\"区域\"]) as cursor:\n",
    "        for row in cursor:\n",
    "            maxX = max([i.X for i in row[0][0] if i ])\n",
    "            maxY = max([i.Y for i in row[0][0] if i ])\n",
    "            minX = min([i.X for i in row[0][0] if i ])\n",
    "            minY = min([i.Y for i in row[0][0] if i ])\n",
    "    \n",
    "            for index,rect in enumerate(rect_locations):\n",
    "                if minX > rect[0] and maxX < rect[2] and minY >rect[1] and maxY < rect[3]:\n",
    "                    assert (int(row[1])-1 < len(k[index])),\"Error:103\"\n",
    "                    k[index][int(row[1])-1] += 1    \n",
    "    count = 0\n",
    "    return_dict = {}\n",
    "    for i in k:\n",
    "        return_dict[i.index(max(i))+1] = rect_locations[count]\n",
    "        count += 1\n",
    "    return return_dict\n",
    "\n",
    "#点到直线的距离，欧氏距离\n",
    "def get_distance_point2line(point, line_ab):\n",
    "    \"\"\"\n",
    "    Args:\n",
    "        point: [x0, y0]\n",
    "        line_ab: [k, b]\n",
    "    \"\"\"\n",
    "    k, b = line_ab\n",
    "    distance = (k * point[0] - point[1] + b) / math.sqrt(k**2 + 1)\n",
    "    return distance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'return_dict' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_34692\\4143635420.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#按范围外、置信区间外、置信区间内分为三类，0=范围外，1=置信区间内 2=范围内置信区间外\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mreturn_dict\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m     \u001b[0mxs\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      4\u001b[0m     \u001b[0mys\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbelong_tb\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m\"0\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'return_dict' is not defined"
     ]
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
